const Observer = require('./observe');

//加法   
Number.prototype.add = function (arg) {
  var r1, r2, m;
  try {
    r1 = this.toString().split(".")[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg.toString().split(".")[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  return (this.mul(m) + arg.mul(m)) / m;
}

//减法   
Number.prototype.sub = function (arg) {
  return this.add(-arg);
}

//乘法   
Number.prototype.mul = function (arg) {
  var m = 0,
    s1 = this.toString(),
    s2 = arg.toString();
  try {
    m += s1.split(".")[1].length
  } catch (e) {}
  try {
    m += s2.split(".")[1].length
  } catch (e) {}
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

//除法   
Number.prototype.div = function (arg) {
  var t1 = 0,
    t2 = 0,
    r1, r2;
  try {
    t1 = this.toString().split(".")[1].length
  } catch (e) {}
  try {
    t2 = arg.toString().split(".")[1].length
  } catch (e) {}
  with(Math) {
    r1 = Number(this.toString().replace(".", ""))
    r2 = Number(arg.toString().replace(".", ""))
    return (r1 / r2) * pow(10, t2 - t1);
  }
}
const compareUp = function (property) {
  return function (object1, object2) { // 属性值为数字  
    var value1 = object1[property];
    var value2 = object2[property];
    return value1 - value2;
  }
}
const compareDown = function (property) {
  return function (object1, object2) { // 属性值为数字  
    var value1 = object1[property];
    var value2 = object2[property];
    return value2 - value1;
  }
}
const top = function (array, key, index) {
  let ret = [];
  // console.log(JSON.stringify(array));
  // console.log(key);
  // console.log(index);
  if (array instanceof Array && array.length > 1) {
    // 数组对象 根据key排序
    if (typeof key == 'string') {
      array.sort(compareDown(key));

    } else {
      // 数值类型 直接排序
      array.sort(function (a, b) {
        return b - a;
      })
    }
    if (index) {
      ret = array.slice(0, index)
    }
    //console.log(JSON.stringify(ret));
  } else {
    ret = array;
  }
  // console.log(JSON.stringify(ret));
  return ret;
}
const moreThan = function(array, key, index){
	let ret = [];
  if (array instanceof Array && array.length > 1) {
	    if (typeof key == 'string') {
	      array.sort(compareDown(key));
	    } else {
	      array.sort(function (a, b) {
	        return b - a;
	      })
	    };
			ret = array.filter(item => {
        return item[key] > index;
     	});
  } else {
    ret = array;
  };
  return ret;
}
const lessThan = function(array, key, index){
	let ret = [];
  if (array instanceof Array && array.length > 1) {
	    if (typeof key == 'string') {
	      array.sort(compareUp(key));
	    } else {
	      array.sort(function (a, b) {
	        return a - b;
	      })
	    };
			ret = array.filter(item => {
        return item[key] < index;
     	});
  } else {
    ret = array;
  }
  return ret;
}
const down = function (array, key, index) {
  let ret = [];
  if (array instanceof Array && array.length > 1) {
    // 数组对象 根据key排序
    if (typeof key == 'string') {
      array.sort(compareUp(key));
    } else {
      // 数值类型 直接排序
      array.sort(function (a, b) {
        return a - b;
      })
    }
    if (index) {
      ret = array.slice(0, index)
    }
  } else {
    ret = array;
  }
  return ret;
}
const operators = {
  'sum': function (array, key) {
    let ret = 0;
    if (array instanceof Array && array.length > 0) {
      // 数组对象 根据key排序
      if (typeof key == 'string') {
        array.forEach(element => {
          if (typeof (element[key]) != 'number') {
            element[key] = parseFloat(element[key]);
          }
          ret = ret.add(element[key]);
        });
      } else {
        // 数值类型 直接排序
        array.forEach(element => {
          if (typeof element == 'number') {
            ret = ret.add(element);
          } else {
            ret = ret.add(parseFloat(element));
          }
        })
      }
    }
    return ret;
  },
  'top': function(array, key, n) {
    n = n || 1;
    return top(array, key, n);
  },
  'top(n)': function() {
  },
  'top10': function (array, key) {
    return top(array, key, 10);
  },
  'top5': function (array, key) {
    return top(array, key, 5);
  },
  'top3': function (array, key) {
    return top(array, key, 3);
  },
  'down': function(array, key, n) {
    n = n || 1;
    return down(array, key, n);
  },
  'down(n)': function() {
  },
  'down10': function (array, key) {
    return down(array, key, 10);
  },
  'down5': function (array, key) {
    return down(array, key, 5);
  },
  'down3': function (array, key) {
    return down(array, key, 3);
  },
  'percent': function (array, key) {
    if (array.length > 0) {
      let number = array[0][key];
      let deci = 2;
      return (Math.round(number * 10000) / 100).toFixed(deci) + '%';
    } else {
      return '';
    }
  },
  'fixed2': function (array, key) {
    if (array.length > 0) {
      let ret = array[0][key];

      if (typeof ret !== 'number') {
        ret = Number(ret);
      }
      ret = ret.toFixed(2);
      return ret;
    } else {
      return '';
    }
  },
  'index': function(array, key, n) {
    let ret = [];
    if (array.length >= n) {
      if (array[n - 1] instanceof Array) {
        ret = array[n - 1]
      } else {
        ret.push(array[n - 1])
      }
    }
    return ret    
  },
  'index(n)': function() {
  },
  'index1': function (array, key) {
    let ret = [];
    if (array.length >= 1) {
      if (array[0] instanceof Array) {
        ret = array[0]
      } else {
        ret.push(array[0])
      }
    }
    return ret
  },
  'index2': function (array, key) {
    let ret = [];
    if (array.length >= 2) {
      if (array[1] instanceof Array) {
        ret = array[1]
      } else {
        ret.push(array[1])
      }
    }
    return ret
  },
  'index3': function (array, key) {
    let ret = [];
    if (array.length >= 3) {
      if (array[2] instanceof Array) {
        ret = array[2]
      } else {
        ret.push(array[2])
      }
    }
    return ret
  },
  'order': function(array, key, n) {
    n = n || 1;
    let ret = top(array, key, n);
    return [ret[n - 1]] || [];
  },
  'order(n)': function() {
  },
  'order1': function (array, key) {
    return top(array, key, 1);
  },
  'order2': function (array, key) {
    let arr = top(array, key, 2);
    return [arr[1]] || []
  },
  'order3': function (array, key) {
    let arr = top(array, key, 3);
    return [arr[2]] || []
  },
  'desc': function(array, key, n) {
    n = n || 1;
    let ret = down(array, key, n);
    return [ret[n - 1]] || [];
  },
  'desc(n)': function() {
  },
  'toTenThousand': function (array, key) {
    if (array.length > 0) {
      let ret = array[0][key];
      let positive = true;
      if (ret == 0) {
        return 0;
      }
      if (ret < 0) {
        ret = Math.abs(ret);
        positive = false;
      }
      let str = '';
      let index = 0;
      let charArr = ['', '万', '亿', '万亿'];
      if (ret > Math.pow(10, 8)) {
        str = (ret / Math.pow(10, 8)).toFixed(2) + charArr[2];
      } else if (ret > Math.pow(10, 4)) {
        str = (ret / Math.pow(10, 4)).toFixed(2) + charArr[1];
      } else {
        str = ret + charArr[0];
      }
      if (!positive) {
        str = '-' + str;
      }
      return str;
      // ret = Math.round((ret / 10000) * 100) / 100;
      // ret = ret + "万";
    } else {
      return '';
    }
  },
  "substr": function (array, key) {
    if (array.length > 0) {
      let ret = array[0][key];
      if (typeof ret !== 'string') {
        ret = String(ret);
      }
      ret = ret.substring(4);
      return ret;
    } else {
      return '';
    }
  },
  "abs": function(array, key) {
    if (array.length > 0) {
      let number = array[0][key];
      return Math.abs(number);
    } else {
      return '';
    }
  },
  "globalContext": function (array, key) {
    if (array.length > 0) {
      let ret = array[0][key];
      //console.log(key + ret);
      //Observer.addContextParams(key, ret);
      return ret;
    } else {
      return '';
    }
  },
  "order(n)": function(array, key) {
    let arr = top(array, key, 3);
    return [arr[2]] || []
  },
  "min": function(array, key) {
    if (array.length > 0) {
      let ret = array[0][key].toString();
      let hour = '';
      let min = '';
      if (ret.length == 4) {

        hour = ret[0] + ret[1];
        min = ret[2] + ret[3];
      } else if (ret.length == 3) {

        hour = ret[0];
        min = ret[1] + ret[2];
      }
      return hour + '时' + min + '分';
    } else {
      return '';
    }
  },
	"yearMonDay": function(array, key) {
    if (array.length > 0) {
      let ret = array[0][key].toString();  //2019-03-14 //2019-03-14 00:15:00
      if(ret.indexOf(':') < 0){
	      let tar = ret.split("-");
	      if(tar.length == 3){
	      	return tar[0] + "年" + tar[1] * 1 + "月" + tar [2] * 1 + "日";
	      } else {
	      	return '';
	      }
      } else {
      	let tar = ret.split(" ");
      	let str = '';
      	if(tar.length == 2){
	      	let tar1 = tar[0].split("-");
		      if(tar1.length == 3){
		      	str = tar1[0] + "年" + tar1[1] * 1 + "月" + tar1[2] * 1 + "日";
		      } else {
		      	return '';
		      };
		      return str + ' ' + tar[1].slice(0, 5)
      	} else {
      		return '';
      	}
      }
    } else {
      return '';
    }
  },
	"monDay": function(array, key) {
    if (array.length > 0) {
      let ret = array[0][key].toString();  //2019-03-14
      let tar = ret.split("-");
    	if(tar.length == 3){
      	return tar[1] * 1 + "月" + tar [2] * 1 + "日";
      } else {
      	return '';
      }
    } else {
      return '';
    }
  },
	"onlyMonth": function(array, key) {
    if (array.length > 0) {
      let ret = array[0][key].toString();  //2019-03-14
      let tar = ret.split("-");
    	if(tar.length == 3){
      	return tar[1] * 1 + "月";
      } else {
      	return '';
      }
    } else {
      return '';
    }
  },
	"onlyYear": function(array, key) {
    if (array.length > 0) {
      let ret = array[0][key].toString();  //2019-03-14
      let tar = ret.split("-");
    	if(tar.length == 3){
      	return tar[0] + "年";
      } else {
      	return '';
      }
    } else {
      return '';
    }
  },
	"max": function(array, key) {
    if (array.length > 0) {
      let tar = Math.max.apply(null, array.map(item => item[key]))
			return tar;
    } else {
      return '';
    }
  },
	"minValue": function(array, key) {
    if (array.length > 0) {
      let tar = Math.min.apply(null, array.map(item => item[key]))
			return tar;
    } else {
      return '';
    }
  },
	'moreThan': function(array, key, n) {
		let tar = Math.min.apply(null, array.map(item => item[key]))
    n = n || tar;
    return moreThan(array, key, n);
  },
	'moreThan(n)': function() {
 	},
	'lessThan': function(array, key, n) {
		let tar = Math.max.apply(null, array.map(item => item[key]))
    n = n || tar;
    return lessThan(array, key, n);
  },
	'lessThan(n)': function() {
  }
}
module.exports = operators;